<?php

namespace app\admin\controller;

use app\admin\model\ApprovesAction;
use app\admin\model\ApprovesPurchase;
use app\admin\model\ApprovesReimburse;
use app\admin\model\UserTitle;
use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 审批流
 *
 * @icon fa fa-circle-o
 */
class Approves extends Backend
{

    /**
     * Approves模型对象
     * @var \app\admin\model\Approves
     */
    protected $model = null;
    protected $approves_action_model = null;
    protected $approves_reimburse_model = null;
    protected $approves_purchase_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Approves;
        $this->approves_action_model = new ApprovesAction();
        $this->approves_reimburse_model = new ApprovesReimburse();
        $this->approves_purchase_model = new ApprovesPurchase();
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with('user')
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list->items() as &$row) {
            $row->approves_text = join(',',\app\admin\model\User::getUsersByIds($row->user_ids));
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 请假
     * type 审批类型:1=请假,2=报销,3=采购
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function leave()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with('user,leave')
            ->where($where)
            ->whereIn('type',1)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list->items() as &$row) {
            $row->approves_text = join(',',\app\admin\model\User::getUsersByIds($row->user_ids));
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 报销审批
     * type 审批类型:1=请假,2=报销,3=采购
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function reimburse()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with('user,reimburse')
            ->where($where)
            ->whereIn('type',2)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list->items() as &$row) {
            $row->approves_text = join(',',\app\admin\model\User::getUsersByIds($row->user_ids));
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /*
     * 报销审批详情
     */
    public function reimburse_detail($ids=null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            $this->view->assign('id', $ids);
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $data = $this->approves_reimburse_model->whereIn('approves_id',$ids)->find();
        $result = ['total' => count($data->content), 'rows' => $data->content];
        return json($result);
    }


    /**
     * 采购审批
     * type 审批类型:1=请假,2=报销,3=采购
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function purchase()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with('user,purchase')
            ->where($where)
            ->whereIn('type',3)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list->items() as &$row) {
            $row->task = \app\admin\model\Tasks::where('id',$row->purchase->task_id)->find();
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /*
     * 采购审批详情
     */
    public function purchase_detail($ids=null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            $this->view->assign('id', $ids);
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $data = $this->approves_purchase_model->whereIn('approves_id',$ids)->find();
        $result = ['total' => count($data->content), 'rows' => $data->content];
        return json($result);
    }

    /*
     * 审批记录
     */
    public function action($ids=null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            $this->view->assign('id', $ids);
            return $this->view->fetch();
        }
        $list = $this->approves_action_model
            ->with('oper')
            ->where('approves_id',$ids)
            ->order('id asc')
            ->paginate(10);
        foreach ($list->items() as &$row) {
            $row->oper->title = UserTitle::get($row->oper['title_id']);
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


}
